<?php
require_once "config.php";
require_once 'class/class.phpmailer.php';
require_once 'extranet_utils.php';

error_reporting(false);
$memberobj=new Member();
$offer_obj = new Offer();
//$active_offers = $offer_obj->list_active_offers();
$id=$_SESSION['member_id'];
$id=isset($id)?$id:$_GET['id'];
$success=false;
if(isset($_POST["updateProfileButton"]))
 {
        $en_key = '&56TCVH%#*P*W&vddbsd&^%';
        $enc = new Encryption;
        $validation_obj = new Validation();
        $validation_error = array();
        $update_profile = $_POST;
       // print_r($update_profile);exit;
        foreach ($update_profile as $key => $value) {
            if($validation_obj->check_blank(trim($value) == true)) {
                    $validation_error[$key] = "<span class='error'>$key is blank</span>";
            }
        }


       //unset($validation_error['username'],$validation_error['password'],$validation_error['terms_and_conditions']);
        unset($validation_error['old_member_password'],$validation_error['new_member_password'],$validation_error['confim_member_password']);
        if($validation_obj->validate_email($update_profile['member_email']) == false) {
            $validation_error['member_email'] = "<span class='error'>Please enter Valid Email</span>";
        }
        $change_pw=false;
        if(trim($update_profile['old_member_password']) != '') {    //check only if the user wants to chnage thte password
            $profile_details_update = $memberobj->get_member_details($id);
            $old_saved_passwaord=isset($profile_details_update['member_password'])?$profile_details_update['member_password']:'';
            $old_member_password=$enc->encrypt($en_key, trim($update_profile['old_member_password']));
            //echo "old pw: ".$old_member_password."<br>";
            //echo "saved pw: ".$old_saved_passwaord."<br>";
            if ($old_saved_passwaord!=$old_member_password){
                $validation_error['old_member_password']="<span class='error'>Incorrect Old Password</span>";
            }
            if (!isset($validation_error['old_member_password'])){ //validate the new password, only if the old password id correct
                if($update_profile['new_member_password'] != $update_profile['confim_member_password']) {
                    $validation_error['confim_member_password'] = "<span class='error'>Password confirmation incorrect</span>";
                }  else {
                    $change_pw=true;
                }        
            }
        }
        
        /*if($memberobj->member_exist(trim($update_profile['member_email'])) != false) {
            $validation_error['member_email'] = "<span class='error'>This email already exist in the system</span>";
        }*/
        if(trim($update_profile['contact_number']) == '') {
            $validation_error['contact_number'] = "<span class='error'>Please enter Contact Number</span>";
        }
        /*if(!isset($update_profile['tc2'])) {
            $validation_error['tc2'] = "<span class='error'>Please Agree to T&C to proceed</span>";
        }*/
        $img = new Securimage();
        $valid = $img->check($_POST['image_code']);

        if($valid == FALSE) {
          $validation_error['image_code'] = "<span class='error'>Sorry, the code you entered was invalid</span>";
        }
      //  print_r($validation_error);exit;
        if(empty($validation_error)) {
            $_SESSION['user_verification'] = 'success';
            $change_pw_array=array();
            if ($change_pw){
                $change_pw_array=array('member_password' => $enc->encrypt($en_key, $update_profile['new_member_password']));
            }
            $verify_details = array('member_email'=>$update_profile['member_email'],
                //'creation_date' => date('Y-m-d H:i:s'),
                'last_update' => date('Y-m-d H:i:s'),
                //'status' => 'P',
                'first_name'=> $update_profile['first_name'],
                'last_name'=> $update_profile['last_name'],
                'contact_number' => $update_profile['contact_number']//,                
                //'member_status' => 'G'

            );
            //print_r($verify_details);
            $verify_details=  array_merge($verify_details, $change_pw_array);
            //echo "---<br>";
            //print_r($verify_details);
            //echo "777<br>";
//exit();
            $memberobj->update_member($verify_details, $id);
            //$_SESSION['member_id'] = $memID;
            $_SESSION['first_name'] = $update_profile['first_name'];
            $_SESSION['last_name'] = $update_profile['last_name'];
            $_SESSION['member_email'] = $update_profile['member_email'];
            $success=true;
            
            }
   
 }else{
     
     //echo "<br>id: ".$id;
     if (isset($id)){
         $profile_details = $memberobj->get_member_details($id);
         //print_r($profile_details);
         /*if ($profile_details){
             
         }else{
             
         }*/
     }
 }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hotels With Offers</title>
<script language="Javascript" type="text/javascript" src="js/jquery-1.4.1.js"></script>
<script language="Javascript" type="text/javascript" src="js/jquery.lwtCountdown-1.0.js"></script>
<script language="Javascript" type="text/javascript" src="js/misc.js"></script>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.validate.js"></script>
<script language="javascript">
    $("#submit_details").validate({
                rules: {
                    new_member_password: "required",
                    confim_member_password: {
                    equalTo: "#new_member_password"
                    }

                }
            });
</script>

</head>

<body>
<div id="wrapper">
    <?php require_once 'top_header.php'; ?>    
<div class="contentPane">
  <div id="sidebar">
    <span class="menu"><img src="images/call.png" alt="Advertise with Hotel with Offers" class="callus"  /></span>
    <div class="joinus">
      <h1>INVITE A FRIEND</h1>
      <h2>Tell a friend and win Rs100.00</h2>
      <div class="joinbtn"><a href="#">LEARN&nbsp;MORE</a></div>
     </div>
    <div class="reserve"><span class="reserveitem">Reserved for :</span> <span class="reserveitem">advertising</span> <span class="reserveitem">latest news</span> <span class="reserveitem">soon ending deals &amp; offers</span></div>
  </div>
  <div class="leftPane">
        <div class="mainMenu"><?php require_once 'main_menu.php'; ?></div>
        <?php require_once 'newsletter_plug.php'; ?>
        <?php if($success):?>
        <div class="success_msg">Profile updated successfully!</div>
        <?php endif; ?>
        <form id="submit_details" name="submit_details" method="post" action="edit_profile.php?id=<?php echo $id;?>">              
              <div class="signUpForm">

          <div class="block">
            <p>First Name</p>
            <p>
              <input name="first_name" class="field" id="first_name" type="text" value="<?= isset($update_profile['first_name'])?$update_profile['first_name']:$profile_details['first_name'];?>" />
              <?php echo isset($validation_error['first_name'])?"Please Enter your first name !":""; ?>
            </p>
          </div>
          <div class="block">
            <p>Last Name</p>
            <p>
              <input name="last_name" class="field" id="last_name" type="text" value="<?= isset($update_profile['last_name'])?$update_profile['last_name']:$profile_details['last_name'];?>"  />
            <?php echo isset($validation_error['last_name'])?"Please Enter your last name !":""; ?>
            </p>
          </div>
          <div class="block">
            <p>Your Email</p>
            <p>
              <input name="member_email" class="field" id="member_email" value="<?= isset($update_profile['member_email'])?$update_profile['member_email']:$profile_details['member_email'];?>" onblur="check_member(this.value);" type="text" />
              &nbsp; <?php echo isset($validation_error['member_email'])?$validation_error['member_email']:""; ?> <span id="email_error"></span></p>
          </div>          
          <div class="block">
            <p>Phone Number</p>
            <p>
              <input name="contact_number" id="contact_number" type="text" value="<?= isset($update_profile['contact_number'])?$update_profile['contact_number']:$profile_details['contact_number'];?>" class="field" />
            <?php echo isset($validation_error['contact_number'])?$validation_error['contact_number']:""; ?>
            </p>
          </div> 
          <div class="reset_pw_block"><span>Change Password</span>
            <div class="block">
                <p>Old Password</p>
                <p>
                <input name="old_member_password" id="old_member_password" type="password" value="" class="field" />
                <?php echo isset($validation_error['old_member_password'])?$validation_error['old_member_password']:""; ?>
                </p>
            </div>
            <div class="block">
                <p>New Password</p>
                <p>
                <input name="new_member_password" id="new_member_password" type="password" value="" class="field" />
                <?php echo isset($validation_error['new_member_password'])?$validation_error['new_member_password']:""; ?>
                </p>
            </div>
            <div class="block">
                <p>Confirm Password</p>
                <p>
                <input name="confim_member_password" id="confim_member_password" type="password" value="" class="field" />
                <?php echo isset($validation_error['confim_member_password'])?$validation_error['confim_member_password']:""; ?>
                </p>
            </div>
          </div>
          <div class="block"> <img src="securimage_show.php?sid=<?php echo md5(time()) ?>" alt="" align="left" id="siimage" style="padding-right: 5px; border: 0" />
            <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="19" height="19" id="SecurImage_as3" align="middle">
              <param name="allowScriptAccess" value="sameDomain" />
              <param name="allowFullScreen" value="false" />
              <param name="movie" value="securimage_play.swf?audio=securimage_play.php&amp;bgColor1=#777&amp;bgColor2=#fff&amp;iconColor=#000&amp;roundedCorner=5" />
              <param name="quality" value="high" />
              <param name="bgcolor" value="#ffffff" />
              <embed src="securimage_play.swf?audio=securimage_play.php&amp;bgColor1=#777&amp;bgColor2=#fff&amp;iconColor=#000&amp;roundedCorner=5" quality="high" bgcolor="#ffffff" width="19" height="19" name="SecurImage_as3" align="middle" allowscriptaccess="sameDomain" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></embed>
              </embed>
            </object>
            <br />
            <!-- pass a session id to the query string of the script to prevent ie caching -->
            <a tabindex="-1" style="border-style: none" href="#" title="Refresh Image" onclick="document.getElementById('siimage').src = 'securimage_show.php?sid=' + Math.random(); return false"><img src="images/refresh.gif" alt="Reload Image" border="0" onclick="this.blur()" align="bottom" /></a> <br class="clear" />
            <div class="block">
              <input type="text" name="image_code" id="image_code" value="Please type in the above code here" class="captcha" />
              <br/>
              <?php echo $validation_error['image_code']; ?></div>
          </div>
          <div class="block">
            <input class="signUpButton" name="updateProfileButton" value="Update" type="submit" class="general_submit" />
          </div>

        </div>
    	</form>
            
    <div class="clear"></div>
</div>
</div>
<div class="clear"></div>
</div>
<div id="footerWrapper">
<?php require_once 'footer.php'; ?>
</div>
</body>
</html>
